package com.example.springboot_first.controller;

import com.example.springboot_first.pojo.ResponseMessage;
import com.example.springboot_first.pojo.User;
import com.example.springboot_first.pojo.dto.UserDto;
import com.example.springboot_first.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

//接口方法返回对象 转换成josn文本
@RestController
//localhost:8080/user/**
@RequestMapping("/user")
public class UserController {
    @Autowired
    IUserService userService;
    //REST
    //增加
    @PostMapping
    public ResponseMessage<User> add(@Validated @RequestBody UserDto user){
        User userNew =userService.add(user);
        return ResponseMessage.success(userNew);
    }
    //查询
    @GetMapping("/{userid}")
    public ResponseMessage get(@PathVariable("userid") Integer userid){
        User userNew = userService.getUser(userid);
        return ResponseMessage.success(userNew);
    }
    //修改
    @PutMapping
    public ResponseMessage edit(@Validated @RequestBody UserDto user){
        User userNew =userService.edit(user);
        return ResponseMessage.success(userNew);
    }
    //删除用户
    @DeleteMapping("/{userid}")
    public ResponseMessage delete(@PathVariable Integer userid){
        userService.delete(userid);
        return ResponseMessage.success();
    }

}
